
# define the config
SHELL = /bin/sh
MAKE = make
CC = gcc
CXX = g++

CFAGS  := -Wall -Werror -g -O2
INC_PATH := -I . -I dll/
RM := rm -f

TARGET := test.exe
LIBS = mylib.dll
LIB_PATH = dll/

SRCEXTS := .c    #.cpp .C

SRC = $(wildcard $(addprefix *,$(SRCEXTS)))
OBJS = $(foreach x,$(SRCEXTS),$(patsubst %$(x),%.o,$(filter %$(x),\
	   $(SRC)))) 
#
.PHONY: all clean cleanall rebuild debug


all:version $(LIBS) $(TARGET)


version:
	@$(CC) --version

#------------------------------------
%.o:%.c
	$(CC) $(CFAGS) $(INC_PATH) -c $<
%.o:%.cpp
	$(CXX) $(CFAGS) $(INC_PATH) -c $<
%.o:%.C
	$(CXX) $(CFAGS) $(INC_PATH) -c $<
#------------------------------------

$(LIBS):
	$(MAKE) -C $(LIB_PATH) all
libclean:
	$(MAKE) -C $(LIB_PATH) clean
#-----------------------------------	
	
$(TARGET):$(OBJS)
	@echo creating $(TARGET)...
ifeq ($(strip $(SRCEXTS)), .c) # C file 
	$(CC) -o $@ $^ -L $(LIB_PATH) $(LIB_PATH)$(LIBS)
else
	$(CXX) -o $@ $^ -L $(LIB_PATH) $(LIB_PATH)$(LIBS)
endif

clean: 
	@echo deleting *.o *.exe *.bak files...
	$(RM) *.o *.exe *.bak
cleanall:clean libclean

rebuild: cleanall all

debug:
	@echo $(SRC)
	@echo $(OBJS)
	
